﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace Billing.Windows
{
    public partial class BulkOrderBoxCostDetails : Window
    {
        BOBDetails details;
        Color cursorEnterColor = Colors.Violet;
        Color cursorLeaveColor = Color.FromArgb(Convert.ToByte("FF", 16), Convert.ToByte("65", 16), Convert.ToByte("65", 16), Convert.ToByte("65", 16));

        public BulkOrderBoxCostDetails(BOBDetails dts)
        {
            InitializeComponent();

            details = dts;
            if (details.EachBoxCost != 0)
                txtBoxCost.Text = details.EachBoxCost.ToString();
            if (details.Qty != 0)
                txtQty.Text = details.Qty.ToString();
            if (details.Advance != 0)
                txtAdvance.Text = details.Advance.ToString();
            if (details.TotalAmt != 0)
                lblTotalAmt.Content = details.TotalAmt.ToString();
            if (details.DiscountByPercentage != 0)
                txtDiscountPer.Text = details.DiscountByPercentage.ToString();
            if (details.DiscountByCost != 0)
                txtDiscountAmt.Text = details.DiscountByCost.ToString();
            discountReason.Text = details.DiscountReason;
            if (details.Balance != 0)
                balance.Text = details.Balance.ToString();

            txtBoxCost.Focus();
            txtBoxCost.SelectAll();
        }

        private void Window_KeyUp(object sender, KeyEventArgs e)
        {
            try
            {
                if (e.Key == Key.Right)
                {
                    TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Next);
                    UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;
                    if (elementWithFocus != null)
                        elementWithFocus.MoveFocus(request);
                }
                else if (e.Key == Key.Left)
                {
                    TraversalRequest request = new TraversalRequest(FocusNavigationDirection.Previous);
                    UIElement elementWithFocus = Keyboard.FocusedElement as UIElement;
                    if (elementWithFocus != null)
                        elementWithFocus.MoveFocus(request);
                }

                if (e.Key == Key.Escape || e.Key == Key.Enter)
                {
                    if (txtBoxCost.Text == string.Empty)
                    {
                        brdrBoxCost.Background = new SolidColorBrush(Colors.Red);
                        return;
                    }
                    else if (txtQty.Text == string.Empty)
                    {
                        brdrQty.Background = new SolidColorBrush(Colors.Red);
                        return;
                    }
                    else
                    {
                        if (!(SetValues()))
                        {
                            MessageBox.Show("Invalid Amounts. Clearing Advance", "Invalid", MessageBoxButton.OK, MessageBoxImage.Error);
                            details.Advance = 0;
                            txtAdvance.Clear();
                            SetValues();
                            return;
                        }
                        this.Close();
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
            }
        }

        private bool SetValues()
        {
            if (details.DiscountByCost != 0)
            {
                details.TotalAmt = (details.EachBoxCost * details.Qty) - details.DiscountByCost;
                details.FinalDiscAmt = details.DiscountByCost;
            }
            else if (details.DiscountByPercentage != 0)
            {
                details.TotalAmt = (details.EachBoxCost * details.Qty * (100 - details.DiscountByPercentage)) / 100;
                details.FinalDiscAmt = details.EachBoxCost * details.Qty * details.DiscountByPercentage / 100;
            }
            else
            {
                details.TotalAmt = details.EachBoxCost * details.Qty;
            }

            lblTotalAmt.Content = details.TotalAmt.ToString();
            details.Balance = details.TotalAmt - details.Advance;
            balance.Text = details.Balance.ToString();
            if (details.Balance < 0)
                return false;
            return true;
        }

        private void txt_GotFocus(object sender, RoutedEventArgs e)
        {
            ((sender as TextBox).Parent as Border).Background = new SolidColorBrush(cursorEnterColor);
        }

        private void txt_LostFocus(object sender, RoutedEventArgs e)
        {
            ((sender as TextBox).Parent as Border).Background = new SolidColorBrush(cursorLeaveColor);
        }

        private void dt_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            ((sender as ComboBox).Parent as Border).Background = new SolidColorBrush(cursorEnterColor);
        }

        private void dt_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            ((sender as ComboBox).Parent as Border).Background = new SolidColorBrush(cursorLeaveColor);
        }

        private void cmbDate_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Right || e.Key == Key.Left)
                e.Handled = true;
            else
                e.Handled = false;
        }

        private void txtEachBoxCost_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (txtBoxCost.Text != string.Empty)
            {
                double val;
                if (!(double.TryParse(txtBoxCost.Text, out val)))
                {
                    MessageBox.Show("Invalid Value Entered", "Invalid", MessageBoxButton.OK, MessageBoxImage.Error);
                    txtBoxCost.Text = string.Empty;
                }
                else
                {
                    details.EachBoxCost = Math.Round(val, 2);
                    txtBoxCost.Text = details.EachBoxCost.ToString();
                }
            }
            else
            {
                details.EachBoxCost = 0;
            }
            SetValues();
        }

        private void txtQty_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (txtQty.Text != string.Empty)
            {
                double val;
                if (!(double.TryParse(txtQty.Text, out val)))
                {
                    MessageBox.Show("Invalid Value Entered", "Invalid", MessageBoxButton.OK, MessageBoxImage.Error);
                    txtQty.Text = string.Empty;
                }
                else
                {
                    details.Qty = (int)val;
                    txtQty.Text = details.Qty.ToString();
                }
            }
            else
            {
                details.Qty = 0;
            }
            SetValues();
        }

        private void txtAdvance_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (txtAdvance.Text != string.Empty)
            {
                double val;
                if (!(double.TryParse(txtAdvance.Text, out val)))
                {
                    MessageBox.Show("Invalid Value Entered", "Invalid", MessageBoxButton.OK, MessageBoxImage.Error);
                    txtAdvance.Text = string.Empty;
                }
                else
                {
                    details.Advance = Math.Round(val, 2);
                    txtAdvance.Text = details.Advance.ToString();
                }
            }
            else
            {
                details.Advance = 0;
            }
            SetValues();
        }

        private void txtDiscountPer_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (txtDiscountPer.Text != string.Empty)
            {
                double val;
                if (!(double.TryParse((sender as TextBox).Text, out val)))
                {
                    MessageBox.Show("Invalid Value Entered", "Invalid", MessageBoxButton.OK, MessageBoxImage.Error);
                    (sender as TextBox).Text = string.Empty;
                }
                else
                {
                    if (val > 50)
                    {
                        MessageBox.Show("Invalid Discount", "Invalid", MessageBoxButton.OK, MessageBoxImage.Error);
                        (sender as TextBox).Text = string.Empty;
                    }
                    else
                    {
                        details.DiscountByPercentage = Math.Round(val, 2);
                        (sender as TextBox).Text = details.DiscountByPercentage.ToString();
                        details.DiscountByCost = 0;
                        txtDiscountAmt.Clear();
                    }
                }
            }
            else
            {
                details.DiscountByPercentage = 0;
            }
            SetValues();
        }

        private void txtDiscountAmt_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (txtDiscountAmt.Text != string.Empty)
            {
                double val;
                if (!(double.TryParse(txtDiscountAmt.Text, out val)))
                {
                    MessageBox.Show("Invalid Value Entered", "Invalid", MessageBoxButton.OK, MessageBoxImage.Error);
                    txtDiscountAmt.Text = string.Empty;
                }
                else
                {
                    if (txtBoxCost.Text == string.Empty)
                    {
                        MessageBox.Show("First Enter Box Cost", "Invalid", MessageBoxButton.OK, MessageBoxImage.Error);
                        txtDiscountAmt.Text = string.Empty;
                    }
                    else if (txtQty.Text == string.Empty)
                    {
                        MessageBox.Show("First Enter Qty", "Invalid", MessageBoxButton.OK, MessageBoxImage.Error);
                        txtDiscountAmt.Text = string.Empty;
                    }
                    else if (details.TotalAmt < val)
                    {
                        MessageBox.Show("Discount should not be greater than Total Amount", "Invalid", MessageBoxButton.OK, MessageBoxImage.Error);
                        txtDiscountAmt.Text = string.Empty;
                    }
                    else
                    {
                        details.DiscountByCost = Math.Round(val, 2);
                        txtDiscountAmt.Text = details.DiscountByCost.ToString();
                        details.DiscountByPercentage = 0;
                        txtDiscountPer.Clear();
                    }
                }
            }
            else
            {
                details.DiscountByCost = 0;
            }
            SetValues();
        }
    }
}
